package gui;

import gui.ShortcutFactory.FactoryType;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.UUID;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.colorchooser.ColorChooserComponentFactory;

import backend.FileIO;
import backend.PathHandler;
import backend.ProgramLauncher;
import backend.PropertiesInterperter;
import backend.XMLCommunicator;

public class XMLConfig extends JFrame {
	private Vector<Shortcut> shortcuts;
	private static XMLConfig instance;
	private PropertiesInterperter properties;

	private XMLConfig(Vector<Shortcut> shortcuts) {
		super();
		this.shortcuts = shortcuts;
		this.properties = new PropertiesInterperter();
		setupThis();
	}


	public static XMLConfig getXMLConfig() {
		return instance;
	}

	public static XMLConfig getXMLConfig(Vector<Shortcut> shortcuts) {
		if (instance==null)
			instance = new XMLConfig(shortcuts);
		return instance;
	}

	public void setShortcuts() {

	}

	private void setupThis() {
		this.setTitle("Shortcut Utility Config");
		this.add(getOuterPanel());

		this.setLocSizeColor();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setupMenu();
	}
	private void setLocSizeColor(){
		this.outerPanel.setBackground(properties.getBGColor());
		this.setSize(properties.getWindowSize());
		this.setLocation(properties.getWindowLocation());	
	}

	private JPanel outerPanel;

	public JPanel getOuterPanel() {
		if (outerPanel == null) {
			outerPanel = new JPanel(new FlowLayout());
			for (Shortcut shortcut : shortcuts) {
				outerPanel.add(shortcut.getComponent());
			}
		}
		return outerPanel;
	}

	public void setupMenu() {

		JMenuBar menuBar = new JMenuBar();

		this.setJMenuBar(menuBar);
		// Menu drops
		JMenu editMenu = new JMenu("Edit");
		JMenu helpMenu = new JMenu("Help");
		JMenu configMenu = new JMenu("Configure");
		menuBar.add(editMenu);
		menuBar.add(configMenu);
		menuBar.add(helpMenu);
		
		// Simple items
		JMenuItem helpAction = new JMenuItem("Help");
		JMenuItem aboutAction = new JMenuItem("About");
		JMenuItem addAction = new JMenuItem("Add");
		JMenuItem saveSizeAction = new JMenuItem("Save window size");
		JMenuItem saveLocationAction = new JMenuItem("Save window location");
		JMenuItem resetLocSizeColorAction = new JMenuItem("Reset window configuration");
		JMenuItem bgcolorAction = new JMenuItem("Choose background color");

		helpMenu.add(helpAction);
		helpMenu.addSeparator();
		helpMenu.add(aboutAction);
		editMenu.add(addAction);
		configMenu.add(saveSizeAction);
		configMenu.add(saveLocationAction);
		configMenu.add(bgcolorAction);
		configMenu.addSeparator();
		configMenu.add(resetLocSizeColorAction);
		helpAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JLabel label = new JLabel(FileIO.loadFile(PathHandler
						.GetLocalPath()
						+ PathHandler.HELP));
				JOptionPane.showMessageDialog(XMLConfig.instance, label,
						"Help", JOptionPane.PLAIN_MESSAGE, null);
			}
		});
		aboutAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JLabel label = new JLabel(FileIO.loadFile(PathHandler
						.GetLocalPath()
						+ PathHandler.ABOUT));
				JOptionPane.showMessageDialog(XMLConfig.instance, label,
						"About", JOptionPane.PLAIN_MESSAGE, null);

			}
		});
		addAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser
						.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				fileChooser.setMultiSelectionEnabled(true);
				File [] file;
				int returnValue = fileChooser.showOpenDialog(XMLConfig
						.getXMLConfig());
				if (returnValue == JFileChooser.APPROVE_OPTION) {
					
					file = fileChooser.getSelectedFiles();
//					String title = JOptionPane
//							.showInputDialog(XMLConfig.getXMLConfig(),
//									"Please enter a name for the shortcut, may be empty");
					for (int i = 0; i < file.length; i++) {
						String title = file[i].getName();
						String id = UUID.randomUUID().toString();
						Shortcut newButton = ShortcutFactory.getFactory(FactoryType.EDITOR).createShortcut(id ,file[i].getAbsolutePath(),
										title, "");
						outerPanel.add(newButton.getComponent());
						XMLCommunicator com = new XMLCommunicator();
						com.addShortcut(id, newButton.getExecPath(), newButton.getComponent()
								.getText(),"");
					}
					
					XMLConfig.getXMLConfig().repaint();
					XMLConfig.getXMLConfig().validate();
				}

			}

		});
		saveSizeAction.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				Dimension windowSize = XMLConfig.getXMLConfig().getSize();
				XMLConfig.getXMLConfig().properties.setWindowSize(windowSize);
			}
		});
		saveLocationAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Point location = XMLConfig.getXMLConfig().getLocation();
				XMLConfig.getXMLConfig().properties.setWindowLocation(location);
			}
		});
		resetLocSizeColorAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				XMLConfig.getXMLConfig().properties.resetLocSizeColor();
				XMLConfig.getXMLConfig().setLocSizeColor();
			}
		});
		bgcolorAction.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Color color = JColorChooser.showDialog(XMLConfig.getXMLConfig(), "Choose background color", properties.getBGColor());
				properties.setBGColor(color);
				XMLConfig.getXMLConfig().outerPanel.setBackground(color);
			}
		});
	}

}
